package scenic.chart;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.axis.ValueAxis;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.AbstractXYItemRenderer;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.chart.title.LegendTitle;
import org.jfree.data.xy.IntervalXYDataset;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RefineryUtilities;

import javax.swing.*;
import java.awt.*;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;


/**
 * Created by scenic on 2017/2/16.
 */
public class ShowSimpleXYChart extends ApplicationFrame {
    private CombinedXYPlot combinedXYPlot;


    private Map<Integer, XYPlot> xyPlotMap = new HashMap<>();

    public ShowSimpleXYChart() {
        this("chart");
    }

    public ShowSimpleXYChart(String title) {
        super(title);

        System.out.println("create");
        JFreeChart chart = createCombinedChart();

        System.out.println("create1");

        JPanel panel = new ChartPanel(chart);
        panel.setPreferredSize(new java.awt.Dimension(500, 800));

        setContentPane(panel);
        System.out.println("create2");

        pack();

        System.out.println("create3");

        RefineryUtilities.centerFrameOnScreen(this);
        System.out.println("create4");

        setVisible(true);
        System.out.println("create5");
        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        System.out.println("finish");


    }

    private JFreeChart createCombinedChart() {


        ValueAxis domainAxis = new NumberAxis("X");
        domainAxis.setLowerMargin(1);

        ValueAxis rangeAxis = new NumberAxis("Y");
        rangeAxis.setLowerMargin(0.1);

        combinedXYPlot = new CombinedXYPlot(domainAxis, rangeAxis);
        combinedXYPlot.setGap(8.0);
        combinedXYPlot.setDomainGridlinePaint(Color.white);
        combinedXYPlot.setDomainGridlinesVisible(true);

        JFreeChart chart = new JFreeChart("ScenicData", JFreeChart.DEFAULT_TITLE_FONT, combinedXYPlot, false);
        chart.setBackgroundPaint(Color.white);
        LegendTitle legend = new LegendTitle(combinedXYPlot);
        chart.addSubtitle(legend);
        return chart;
    }


    private XYPlot createPlot(int key) {
        if (xyPlotMap.containsKey(key)) {
            return xyPlotMap.get(key);
        } else {

            XYPlot mXYPlot = new XYPlot();
            AbstractXYItemRenderer renderer = new XYLineAndShapeRenderer(true, false);
            mXYPlot.setRenderer(renderer);
            if(key % 2 == 0){
                renderer.setSeriesPaint(key, Color.blue);
            }else {
                renderer.setSeriesPaint(key, Color.green);
            }


            mXYPlot.setBackgroundPaint(Color.lightGray);
            mXYPlot.setDomainGridlinePaint(Color.white);
            mXYPlot.setRangeGridlinePaint(Color.white);
            xyPlotMap.put(key, mXYPlot);
            combinedXYPlot.add(mXYPlot,key + 3);
            return mXYPlot;
        }
    }



    public void update(Map<Number, Number>... dataTmp) {
        for (int i = 0; i < dataTmp.length; i++) {
            XYSeries series = new XYSeries("C " + i);
            Map<Number, Number> data = dataTmp[i];
            data.forEach((x, y) -> series.add(x, y));
            IntervalXYDataset collection = new XYSeriesCollection(series);
            createPlot(i).setDataset(collection);
        }

    }



    public static void main(String args[]) {



        ShowSimpleXYChart simpleXYChart = new ShowSimpleXYChart("Log");
        LinkedHashMap<Number, Number> map = new LinkedHashMap();
        map.put(250, 0d);
        map.put(500, 0d);
        map.put(1000, 0.2);
        map.put(2000, 0.4);
        map.put(4000, 2.4);
        map.put(8000, 19.4);
        map.put(16000, 154.2);
        map.put(32000, 1229.6);


        LinkedHashMap<Number, Number> map2 = new LinkedHashMap();
        map.forEach((x, y) -> {

            double yy = Math.log((y.doubleValue() + 1))*100;
            System.out.println(yy
            );
            map2.put(x, yy);
        });


        simpleXYChart.update(map, map2);

    }


}
